import Vue from "vue";
import VueRouter from "vue-router";

const originalPush = VueRouter.prototype.push

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location){
return originalPush.call(this , location).catch(err=>err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location){
return originalReplace.call(this , location).catch(err=>err)
}

Vue.use(VueRouter);


                        

const routes = [{
    path: '/',
    redirect: '/index',
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home.vue'),
    children: [{
      path: '/index',
      name: 'index',
      component: () => import('../views/indexs/index.vue'),
      meta: {
        title: '粮仓安全智慧预警数据中心',
      },
    }]
  },
  {
    path: "/loginqiti",
    name: 'loginqiti',
    component: () => import('../views/qiti/Login.vue'),
    meta: {
      title: '登录页面'
    }
  },

  {
    path: "/qiti",
    name: 'qiti',
    component: () => import('../views/qiti/index.vue'),
    children: [
      {
        path: 'homeqiti',
        name: 'homeqiti',
        component: () => import('../views/qiti/HomeM.vue'),
        meta: {
          title: '气体检测'
        },
      },    
      {
        path: "qitiDetail",
        name: 'qitiDetail',
        component: () => import('../views/qiti/qitiDetail.vue'),
        meta: {
          title: '气体咨询详情页面'
        }
      },
      {
        path: "zixun",
        name: 'zixun',
        component: () => import('../views/qiti/zixun.vue'),
        meta: {
          title: '气体咨询页面'
        }
      },
      {
        path: "about",
        name: 'about',
        component: () => import('../views/qiti/about.vue'),
        meta: {
          title: '关于我们'
        }
      },
      {
        path: '/qiti',
        redirect: '/qiti/homeqiti'
      }
    ]
  },
];



const router = new VueRouter({
  mode: "hash",
  // base: process.env.BASE_URL,
  routes
});


const whiteMenu = ['loginqiti', 'index']
router.beforeEach((to, from, next) => {
  let title = to.meta.title;
  // 标签栏标题
  document.title = title;
  let isLogin = localStorage.getItem('token')
  if(isLogin) {
    // 如果处于已经登录状态
    next()
  } else {
    // 没有登录
    // 可以访问白名单
    if(whiteMenu.includes(to.name)) {
      next() 
    } else {
      next("/loginqiti")
    }
  }
})

export default router;